home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-05-22 | 18.4 KB | 716 lines | [TEXT/MPCC] |
- //
- // File: MacApplication.c
- //
- // Contains: QuickDraw 3D support for QuickTime VR movies.
- //
- // Written by: Tim Monroe
- // Based (heavily!) on the MovieShell code written by Apple DTS
- //
- // Copyright: © 1994-1996 by Apple Computer, Inc., all rights reserved.
- //
- // Change History (most recent first):
- //
- // <11> 01/17/97 rtm worked on InitApplicationWindowObject
- // <10> 01/14/97 rtm added static texture support
- // <9> 01/13/97 rtm added menu cases for stopping/starting sound on embedded movies
- // <8> 01/12/97 rtm added ApplicationMCActionFilterProc to consolidate mc action handling
- // <7> 01/09/97 rtm added initial FOV reset in InitApplicationWindowObject;
- // moved rendering code into prescreen procedure
- // <6> 01/06/97 rtm DoIdle now renders and updates only when necessary;
- // updates cursor only for front window
- // <5> 12/17/96 rtm added testing for QuickDraw 3D in appropriate places
- // <4> 12/13/96 rtm added 3D object support (see TestFunctions.c)
- // <3> 12/02/96 rtm added cursor updating to DoIdle
- // <2> 11/27/96 rtm conversion to personal coding style;
- // added preliminary QTVR support
- // <1> 12/21/94 khs first file
- //
- //
- // TODO:
- // + rethink DoIdle calling QTVRUpdate every null event
-
- // header files
- #include "MacApplication.h"
- #include "MacFramework.h"
- #include "AppConfiguration.h"
- #include "DTSQTUtilities.h"
- #include "QTVRUtilities.h"
- #include <Resources.h>
-
- // header file for the specific test functions.
- #include "TestFunctions.h"
-
- // global variables
- long gMaxMilliSecToUse = kMaxMilliSecToUse;
- Boolean gQTVRMgrIsPresent = false; // is the QuickTime VR Manager available?
- Boolean gHasQuickDraw3D = false; // is QuickDraw 3D available?
-
-
- //////////
- //
- // InitApplication
- // Do any application-specific initialization.
- //
- //////////
-
- void InitApplication (void)
- {
- // make sure that the QTVR Manager is present in the present operating environment
- if (QTVRUtils_IsQTVRMgrInstalled()) {
- gQTVRMgrIsPresent = true;
- } else {
- ShowWarning("\pThe QuickTime VR Manager cannot be found.", 0);
- ExitToShell();
- }
-
- // initialize 3D object capabilities
- VR3DObjects_Init();
- }
-
-
- //////////
- //
- // StopApplication
- // Do any application-specific shutdown.
- //
- //////////
-
- void StopApplication (void)
- {
- // shut down 3D object capabilities, if enabled
- VR3DObjects_Stop();
- }
-
-
- //////////
- //
- // DoIdle
- // Do any processing that can/should occur at idle time.
- //
- //////////
-
- void DoIdle (WindowRef theWindow)
- {
- GrafPtr mySavedPort;
- WindowObject myWindowObject;
- MovieController myMC = NULL;
- Point myPoint;
-
- GetPort(&mySavedPort);
- SetPort(theWindow);
-
- myWindowObject = (WindowObject)GetWRefCon(theWindow);
- if (myWindowObject != NULL) {
-
- myMC = (**myWindowObject).fController;
- if (myMC != NULL) {
-
- MoviesTask(MCGetMovie(myMC), gMaxMilliSecToUse);
-
- // restore the cursor to the arrow if it's outside the front movie window
- // and the mouse button isn't still down;
- // this is needed only for VR movies, but should probably always be done
- if (theWindow == FrontWindow()) {
- GetMouse(&myPoint);
- if (!PtInMovie(MCGetMovie(myMC), myPoint) && !StillDown())
- InitCursor();
- }
- }
- }
-
- // @@@INSERT ANY OTHER IDLE-BASED FUNCTIONALITY HERE
-
- // 3D object overlay (and QuickTime movies) support
- if (myWindowObject != NULL) {
- if (gHasQuickDraw3D) {
- QTVRInstance myInstance;
-
- myInstance = (**myWindowObject).fInstance;
- if (myInstance != NULL) {
- if (QTVRGetNodeType(myInstance, kQTVRCurrentNode) == kQTVRPanoramaType) {
- QTVRUpdate(myInstance, kQTVRCurrentMode);
- } else {
- //VR3DObjects_PrescreenRoutine(myInstance, myWindowObject);
- }
- }
- }
- }
-
- SetPort(mySavedPort);
- }
-
-
- //////////
- //
- // DoUpdateWindow
- // Update the specified window.
- //
- //////////
-
- void DoUpdateWindow (WindowRef theWindow, Rect *theRefreshArea)
- {
- GrafPtr mySavedPort;
-
- if (theWindow != NULL) {
-
- GetPort(&mySavedPort);
- SetPort(theWindow);
-
- BeginUpdate(theWindow);
-
- EraseRect(theRefreshArea); // this is important for non-rectangular movies
-
- // @@@INSERT WINDOW-SPECIFIC DRAWING FUNCTIONALITY HERE
-
- EndUpdate(theWindow);
- SetPort(mySavedPort);
- }
- }
-
-
- //////////
- //
- // HandleContentClick
- // Handle mouse button clicks in the specified window.
- //
- //////////
-
- void HandleContentClick (WindowRef theWindow, EventRecord *theEvent)
- {
- #pragma unused(theEvent)
-
- GrafPtr mySavedPort;
-
- GetPort(&mySavedPort);
- SetPort(theWindow);
-
- // @@@INSERT APPLICATION-SPECIFIC CONTENT CLICKING FUNCTIONALITY HERE
-
- SetPort(mySavedPort);
- }
-
-
- //////////
- //
- // HandleQTVRKeyPress
- // Handle QuickTime VR-specific key presses.
- // Returns true if the key press was handled, false otherwise.
- //
- //////////
-
- Boolean HandleQTVRKeyPress (EventRecord *theEvent)
- {
- Boolean isHandled = true;
- char myCharCode;
-
- myCharCode = theEvent->message & charCodeMask;
-
- switch (myCharCode) {
-
- default:
- isHandled = false;
- break;
- }
-
- return(isHandled);
- }
-
-
- //////////
- //
- // CreateMovieWindow
- // Create a window to display a movie in.
- //
- //////////
-
- WindowRef CreateMovieWindow (Rect *theRect, Str255 theTitle)
- {
- WindowRef myWindow;
-
- myWindow = NewCWindow(NULL, theRect, theTitle, false, noGrowDocProc, (WindowPtr)-1L, true, 0);
- return(myWindow);
- }
-
-
- //////////
- //
- // HandleApplicationMenu
- // Handle selections in the application's menus.
- //
- //////////
-
- void HandleApplicationMenu (short theMenuID, short theMenuItem)
- {
- MovieController myMC = NULL;
- ApplicationDataHdl myAppData;
-
- myAppData = (ApplicationDataHdl)GetAppDataFromFrontWindow();
- if (myAppData == NULL)
- return;
-
- switch (theMenuID) {
- case mTesting:
- switch(theMenuItem) {
- case iTest1: // load new picture texture
- case iTest2: // load new movie texture
- // delete the existing texture structure, since we'll soon create another one
- if ((**myAppData).fTexture != NULL) {
- VR3DTexture_Delete((**myAppData).fTexture);
- (**myAppData).fTexture = NULL;
- }
-
- (**myAppData).fTextureIsMovie = (theMenuItem == iTest2);
- (**myAppData).fTexture = VR3DTexture_New(theMenuItem == iTest2);
-
- if ((**myAppData).fTexture != NULL)
- VR3DTexture_AddToGroup((**myAppData).fTexture, (**myAppData).fModel);
-
- // show the volume control button, if a sound track exists in the QT movie
- (**myAppData).fQTMovieHasSound = false;
- if ((**myAppData).fTexture != NULL) {
- if ((**(**myAppData).fTexture).fMovie) {
- if (QTUMediaTypeInTrack((**(**myAppData).fTexture).fMovie, SoundMediaType)) {
- (**myAppData).fQTMovieHasSound = true;
- myMC = GetMCFromFrontWindow();
- if (myMC != NULL) {
- QTVRUtils_ShowControllerButton(myMC, kQTVRSpeakerButton);
- }
- }
- }
- }
- break;
-
- case iTest3: // toggle object animation
- (**myAppData).fObjectIsAnimated = !(**myAppData).fObjectIsAnimated;
- break;
-
- case iTest4: // stop embedded movie sound
- if ((**myAppData).fTexture != NULL)
- if ((**(**myAppData).fTexture).fMovie != NULL)
- QTVRUtils_StopMovieSound((**(**myAppData).fTexture).fMovie);
- break;
-
- case iTest5: // start embedded movie sound
- if ((**myAppData).fTexture != NULL)
- if ((**(**myAppData).fTexture).fMovie != NULL)
- QTVRUtils_PlayMovieSound((**(**myAppData).fTexture).fMovie);
- break;
-
- case iTest6:
- VR3DObjects_FadeObjects(myAppData, true);
- break;
-
- case iTest7:
- VR3DObjects_FadeObjects(myAppData, false);
- break;
- }
- break;
-
- case mObjects: // select a new 3D object
-
- // dispose of the existing 3D object
- if ((**myAppData).fModel != NULL)
- Q3Object_Dispose((**myAppData).fModel);
-
- switch(theMenuItem) {
- case iBox:
- (**myAppData).fModel = VR3DObjects_CreateModel(kQ3GeometryTypeBox);
- break;
-
- case iCone:
- (**myAppData).fModel = VR3DObjects_CreateModel(kQ3GeometryTypeCone);
- break;
-
- case iCylinder:
- (**myAppData).fModel = VR3DObjects_CreateModel(kQ3GeometryTypeCylinder);
- break;
-
- case iEllipsoid:
- (**myAppData).fModel = VR3DObjects_CreateModel(kQ3GeometryTypeEllipsoid);
- break;
-
- case iTorus:
- (**myAppData).fModel = VR3DObjects_CreateModel(kQ3GeometryTypeTorus);
- break;
-
- case iPolyhedron:
- (**myAppData).fModel = VR3DObjects_CreateModel(kQ3GeometryTypePolyhedron);
- break;
-
- case i3DMFFile:
- VR3DObjects_GetModelFromFile();
- break;
-
- default:
- break;
- }
-
- // restore any existing texture
- if ((**myAppData).fTexture != NULL)
- VR3DTexture_AddToGroup((**myAppData).fTexture, (**myAppData).fModel);
-
- // set some default 3D object properties
- if (theMenuItem != i3DMFFile)
- Q3Point3D_Set(&(**myAppData).fGroupCenter, 0.0, 0.0, -k3DObjectDistance);
- (**myAppData).fObjectIndex = theMenuItem;
- (**myAppData).fMustRender = true;
- break;
- }
- }
-
-
- //////////
- //
- // AdjustApplicationMenus
- // Adjust state of items in the application's menus.
- //
- //////////
-
- void AdjustApplicationMenus (void)
- {
- WindowRef myWindow = NULL;
- MovieController myMC = NULL;
-
- myMC = GetMCFromFrontWindow();
- if (myMC != NULL) {
- EnableItem(GetMHandle(mTesting), iTest1);
- EnableItem(GetMHandle(mTesting), iTest2);
- EnableItem(GetMHandle(mTesting), iTest3);
- EnableItem(GetMHandle(mTesting), iTest4);
- EnableItem(GetMHandle(mTesting), iTest5);
- EnableItem(GetMHandle(mTesting), iTest6);
- EnableItem(GetMHandle(mTesting), iTest7);
- EnableItem(GetMHandle(mObjects), iBox);
- EnableItem(GetMHandle(mObjects), iCone);
- EnableItem(GetMHandle(mObjects), iCylinder);
- EnableItem(GetMHandle(mObjects), iEllipsoid);
- EnableItem(GetMHandle(mObjects), iTorus);
- EnableItem(GetMHandle(mObjects), iPolyhedron);
- EnableItem(GetMHandle(mObjects), i3DMFFile);
- } else {
- DisableItem(GetMHandle(mTesting), iTest1);
- DisableItem(GetMHandle(mTesting), iTest2);
- DisableItem(GetMHandle(mTesting), iTest3);
- DisableItem(GetMHandle(mTesting), iTest4);
- DisableItem(GetMHandle(mTesting), iTest5);
- DisableItem(GetMHandle(mTesting), iTest6);
- DisableItem(GetMHandle(mTesting), iTest7);
- DisableItem(GetMHandle(mObjects), iBox);
- DisableItem(GetMHandle(mObjects), iCone);
- DisableItem(GetMHandle(mObjects), iCylinder);
- DisableItem(GetMHandle(mObjects), iEllipsoid);
- DisableItem(GetMHandle(mObjects), iTorus);
- DisableItem(GetMHandle(mObjects), iPolyhedron);
- DisableItem(GetMHandle(mObjects), i3DMFFile);
- }
-
- DisableItem(GetMHandle(mFile), iNew); // we don't allow creating new VR files here...
-
- // if QuickDraw 3D isn't available, don't allow reading models or embedding movies
- if (!gHasQuickDraw3D) {
- DisableItem(GetMHandle(mTesting), iTest3);
- DisableItem(GetMHandle(mTesting), iTest4);
- DisableItem(GetMHandle(mTesting), iTest5);
- DisableItem(GetMHandle(mObjects), iBox);
- DisableItem(GetMHandle(mObjects), iCone);
- DisableItem(GetMHandle(mObjects), iCylinder);
- DisableItem(GetMHandle(mObjects), iEllipsoid);
- DisableItem(GetMHandle(mObjects), iTorus);
- DisableItem(GetMHandle(mObjects), iPolyhedron);
- DisableItem(GetMHandle(mObjects), i3DMFFile);
- }
-
- // if the front window has no embedded movie or the movie has no sound track,
- // disable sound-adjusting menu items;
- // if the front window has a predefined 3D object,
- // check appropriate item in Objects menu
- {
- ApplicationDataHdl myAppData;
- short myIndex;
-
- // assume there's no movie or no sound
- DisableItem(GetMHandle(mTesting), iTest4);
- DisableItem(GetMHandle(mTesting), iTest5);
-
- myAppData = (ApplicationDataHdl)GetAppDataFromFrontWindow();
- if (myAppData != NULL) {
-
- // handle sound-related items
- if (((**myAppData).fTexture != NULL) && (**myAppData).fQTMovieHasSound) {
- EnableItem(GetMHandle(mTesting), iTest4);
- EnableItem(GetMHandle(mTesting), iTest5);
- }
-
- // handle checkmarks in Objects menu
- for (myIndex = iBox; myIndex <= i3DMFFile; myIndex++) {
- CheckItem(GetMHandle(mObjects), myIndex, (myIndex == (**myAppData).fObjectIndex));
- }
- }
-
- }
-
- }
-
-
- //////////
- //
- // AddControllerFunctionality
- // Configure the movie controller.
- //
- //////////
-
- void AddControllerFunctionality (MovieController theMC)
- {
- long myControllerFlags;
-
- // CLUT table use
- MCDoAction(theMC, mcActionGetFlags, &myControllerFlags);
- MCDoAction(theMC, mcActionSetFlags, (void *)(myControllerFlags | mcFlagsUseWindowPalette));
-
- // enable keyboard event handling
- MCDoAction(theMC, mcActionSetKeysEnabled, (void *)true);
-
- // disable drag support
- MCDoAction(theMC, mcActionSetDragEnabled, (void *)false);
- }
-
-
- //////////
- //
- // GetQTVRInstanceFromFrontWindow
- // Get the QTVRInstance associated with the front window.
- //
- //////////
-
- QTVRInstance GetQTVRInstanceFromFrontWindow (void)
- {
- QTVRInstance myInstance = NULL;
- WindowRef myWindow = NULL;
- WindowObject myWindowObject = NULL;
-
- myWindow = FrontWindow();
- if (myWindow == NULL)
- return(NULL);
-
- if (!IsAppWindow(myWindow))
- return(NULL);
-
- myWindowObject = (WindowObject)GetWRefCon(myWindow);
- if (myWindowObject == NULL)
- return(NULL);
-
- HLockHi((Handle)myWindowObject);
-
- // make sure this is a window object
- if (!IsWindowObjectOurs(myWindowObject))
- return(NULL);
-
- myInstance = (**myWindowObject).fInstance;
-
- HUnlock((Handle)myWindowObject);
-
- return(myInstance);
- }
-
-
- //////////
- //
- // InitApplicationWindowObject
- // Do any application-specific initialization of the window object.
- //
- //////////
-
- void InitApplicationWindowObject (WindowObject theWindowObject)
- {
- Track myQTVRTrack = NULL;
- OSErr myErr = noErr;
- Movie myMovie = NULL;
- MovieController myMC = NULL;
- QTVRInstance myInstance = NULL;
-
- if (theWindowObject == NULL)
- return;
-
- // find the QTVR track
- myMovie = (**theWindowObject).fMovie;
- myMC = (**theWindowObject).fController;
- myQTVRTrack = QTVRGetQTVRTrack(myMovie, 1);
-
- myErr = QTVRGetQTVRInstance(&myInstance, myQTVRTrack, myMC);
- (**theWindowObject).fInstance = myInstance;
-
- // do any app-specific window configuration
- if (myInstance != NULL) {
-
- // set unit to radians
- myErr = QTVRSetAngularUnits(myInstance, kQTVRRadians);
-
- // do any 3D-specific configuration
- if (gHasQuickDraw3D) {
- ApplicationDataHdl myAppData;
-
- // do QuickDraw 3D window configuration
- myAppData = VR3DObjects_InitWindowData(theWindowObject);
- (**theWindowObject).fAppData = (Handle)myAppData;
-
- // install an intercept procedure to handle signal a changed pan/tilt/zoom for 3D overlay
- VR3DObjects_InstallInterceptRoutine(myInstance, theWindowObject);
-
- // for panoramic nodes,
- // install a prescreen buffer imaging complete procedure to image the 3D overlay
- if (QTVRGetNodeType(myInstance, kQTVRCurrentNode) == kQTVRPanoramaType)
- VR3DObjects_InstallPrescreenRoutine(myInstance, theWindowObject);
-
- // initialize QD3D camera's aspect ratio
- VR3DObjects_SetCameraAspectRatio(theWindowObject);
-
- // initialize QD3D camera's point-of-interest and FOV
- VR3DObjects_SetCamera(theWindowObject);
-
- if (myAppData != NULL)
- (**myAppData).fMustRender = true;
- QTVRUpdate(myInstance, kQTVRCurrentMode);
- }
- }
- }
-
-
- //////////
- //
- // RemoveApplicationWindowObject
- // Do any application-specific clean-up of the window object.
- //
- //////////
-
- void RemoveApplicationWindowObject (WindowObject theWindowObject)
- {
- OSErr myErr = noErr;
- QTVRInstance myInstance = NULL;
- ApplicationDataHdl myAppData;
-
- // do some preliminary parameter checking
- if (theWindowObject == NULL)
- return;
-
- myAppData = (ApplicationDataHdl)GetAppDataFromWindowObject(theWindowObject);
-
- // QuickTime VR clean-up
- myInstance = (**theWindowObject).fInstance;
- if (myInstance != NULL) {
-
- }
-
- // QuickDraw 3D clean-up
- if (gHasQuickDraw3D) {
- if (myAppData != NULL) {
- if ((**myAppData).fView)
- Q3Object_Dispose((**myAppData).fView);
- if ((**myAppData).fModel)
- Q3Object_Dispose((**myAppData).fModel);
- if ((**myAppData).fInterpolation)
- Q3Object_Dispose((**myAppData).fInterpolation);
- if ((**myAppData).fBackFacing)
- Q3Object_Dispose((**myAppData).fBackFacing);
- if ((**myAppData).fFillStyle)
- Q3Object_Dispose((**myAppData).fFillStyle);
- VR3DTexture_Delete((**myAppData).fTexture);
-
- // dispose of GWorlds used by this window object
- DisposeGWorld((**myAppData).fGWorld);
- }
- }
-
- // application clean-up
- if (myAppData != NULL)
- DisposeHandle((Handle)myAppData);
-
- // DoDestroyMovieWindow in MacFramework.c releases the window object itself
- }
-
-
- //////////
- //
- // ApplicationMCActionFilterProc
- // Intercept some mc actions for the QuickTime VR movie controller.
- //
- //////////
-
- pascal Boolean ApplicationMCActionFilterProc (MovieController theMC, short theAction, void *theParams, WindowRef theWindow)
- {
- Boolean isHandled = false;
- WindowObject myWindowObject;
- ApplicationDataHdl myAppData;
-
- // get the window object associated with the specified window
- myWindowObject = (WindowObject)GetWRefCon(theWindow);
-
- // do some preliminary parameter checking
- if (myWindowObject == NULL)
- return(isHandled);
-
- myAppData = (ApplicationDataHdl)GetAppDataFromWindowObject(myWindowObject);
-
- switch (theAction) {
-
- // handle window resizing
- case mcActionControllerSizeChanged: {
- Rect myRect;
-
- // get the new boundary rectangle of the QTVR controller and set window size
- MCGetControllerBoundsRect(theMC, &myRect);
- SizeWindow((WindowPtr)theWindow, myRect.right - myRect.left, myRect.bottom - myRect.top, true);
-
- // because the window size has changed,
- // we need to recalculate QD3D camera's aspect ratio and resize QD3D draw context
- if (myAppData != NULL) {
- VR3DObjects_SetCameraAspectRatio(myWindowObject);
- VR3DObjects_UpdateDrawContext(myWindowObject);
- (**myAppData).fMustRender = true;
- }
- break;
- }
-
- // look for any sound-related actions and route them to the embedded QuickTime movie,
- // not to the QTVR controller
- case mcActionGetVolume:
- case mcActionSetVolume: {
- short myVolume, *myVolumePtr;
- Movie myMovie;
-
- if (myAppData == NULL)
- break;
-
- if (!(**myAppData).fQTMovieHasSound)
- break;
-
- if ((**myAppData).fTexture != NULL) {
- if ((**(**myAppData).fTexture).fMovie != NULL) {
-
- myMovie = (**(**myAppData).fTexture).fMovie;
-
- if (theAction == mcActionGetVolume) {
- myVolume = GetMovieVolume(myMovie);
- theParams = &myVolume;
- }
-
- if (theAction == mcActionSetVolume) {
- myVolumePtr = (short *)theParams;
- myVolume = *myVolumePtr;
- SetMovieVolume(myMovie, myVolume);
- }
-
- isHandled = true;
- }
- }
-
- break;
- }
-
- default:
- break;
-
- } // switch(theAction)
-
- return(isHandled);
- }
-